package com.gxy.learn.backup.shiroauth.shiro.config;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.pam.AtLeastOneSuccessfulStrategy;
import org.apache.shiro.realm.Realm;


/**
 * Description(继承多realms策略AtLeastOneSuccessfulStrategy进行自己实现)
 * 多个realm时 登录就会 org.apache.shiro.authc.pam.ModularRealmAuthenticator#doMultiRealmAuthentication 通过这个方法进行执行，如果所有的对应的realm都返回认证异常或者null的话，就会出现以下错误
 * 解决 Authentication token of type [class org.apache.shiro.authc.UsernamePasswordToken] could not be authenticated by any configured realms. Please ensure that at least one realm can authenticate these tokens.
 *
 * @author: Gao xueyong
 * Create at: 2022/2/12 下午11:51
 */
public class MyAtLeastOneSuccessfulStrategy extends AtLeastOneSuccessfulStrategy {
    @Override
    public AuthenticationInfo afterAttempt(Realm realm, AuthenticationToken token, AuthenticationInfo singleRealmInfo, AuthenticationInfo aggregateInfo, Throwable t) throws AuthenticationException {
        if (t != null && t instanceof AuthenticationException) {
            throw (AuthenticationException) t;
        }
        return super.afterAttempt(realm, token, singleRealmInfo, aggregateInfo, t);
    }
}
